//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Windows.Input;

namespace OpenLS.Spreadsheet.UI.Internals
{
    internal static class CommandHelpers
    {
        internal static void RegisterCommandHandler(Type controlType, RoutedCommand command,
                                                    ExecutedRoutedEventHandler executedRoutedEventHandler,
                                                    CanExecuteRoutedEventHandler canExecuteRoutedEventHandler)
        {
            registerCommandHandlerImp(controlType, command, executedRoutedEventHandler, canExecuteRoutedEventHandler,
                                      new InputGesture[]
                                          {
                                          });
        }
        internal static void RegisterCommandHandler(Type controlType, RoutedCommand command,
                                                    ExecutedRoutedEventHandler executedRoutedEventHandler,
                                                    CanExecuteRoutedEventHandler canExecuteRoutedEventHandler, Key key)
        {
            registerCommandHandlerImp(controlType, command, executedRoutedEventHandler, canExecuteRoutedEventHandler,
                                      new InputGesture[]
                                          {
                                              new KeyGesture(key)
                                          });
        }

        internal static void RegisterCommandHandler(Type controlType, RoutedCommand command,
                                                    ExecutedRoutedEventHandler executedRoutedEventHandler,
                                                    CanExecuteRoutedEventHandler canExecuteRoutedEventHandler, Key key,
                                                    ModifierKeys modifierKeys)
        {
            registerCommandHandlerImp(controlType, command, executedRoutedEventHandler, canExecuteRoutedEventHandler,
                                      new InputGesture[]
                                          {
                                              new KeyGesture(key, modifierKeys)
                                          });
        }


        private static void registerCommandHandlerImp(Type controlType, RoutedCommand command,
                                                      ExecutedRoutedEventHandler executedRoutedEventHandler,
                                                      CanExecuteRoutedEventHandler canExecuteRoutedEventHandler,
                                                      params InputGesture[] inputGestures)
        {
            CommandManager.RegisterClassCommandBinding(controlType,
                                                       new CommandBinding(command, executedRoutedEventHandler,
                                                                          canExecuteRoutedEventHandler));
            if (inputGestures != null)
            {
                for (var i = 0; i < inputGestures.Length; i++)
                {
                    CommandManager.RegisterClassInputBinding(controlType, new InputBinding(command, inputGestures[i]));
                }
            }
        }
    }
}